package br.com.games.pyx.scenes;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.anddev.andengine.entity.layer.Layer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.TextureManager;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.util.Log;
import br.com.games.pyx.Main;
import br.com.games.pyx.atores.game.Ix;
import br.com.games.pyx.atores.game.Toupety;
import br.com.games.pyx.mosaico.CarregadorMosaico;
import br.com.games.pyx.mosaico.CarregadorMosaicoAnimado;
import br.com.games.pyx.mosaico.Mosaico;
import br.com.games.pyx.mosaico.MosaicoAnimado;
import br.com.games.pyx.singleton.FabricaRecursos;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactListener;

public class Cenario extends Scene implements IOnScreenControlListener, ContactListener, IAnalogOnScreenControlListener {

	private PhysicsWorld world;
	
	private AnalogOnScreenControl control;
	
	private Toupety toupety;
	
	private Layer fundo = new Layer();
	private Layer meio = new Layer();
	private Layer animados = new Layer();
	private Layer frente = new Layer();
	
	
	public Cenario() {
		super(1);
	}

	public void loadResourcers(String arquivo) {
		//teste abaixo
		
		this.world = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);
		
		this.world.setContactListener(this);
		this.registerUpdateHandler(world);
		
		TextureRegion ctrlTxt = FabricaRecursos.get().getScreenControlTextureRegion();
		TextureRegion ctrlKTxt = FabricaRecursos.get().getScreenControlKnobTextureRegion();
		Camera cam = FabricaRecursos.get().getCamera();
		
		this.attachChild(fundo);
		this.attachChild(meio);
		this.attachChild(animados);
		this.attachChild(frente);
		
		toupety = FabricaRecursos.get().getToupety();
		toupety.setPosition(200,100);
		toupety.getActual().animate(80);		
		toupety.registerBody(world);
		
		Ix ix = FabricaRecursos.get().getIx();
		ix.setPosition(220,100);
		ix.getActual().animate(80);
		ix.registerBody(world);
		
		frente.attachChild(toupety);
		meio.attachChild(ix);
		
		/*TMXLoader load = new TMXLoader(FabricaRecursos.get().getMainContext(), FabricaRecursos.get().getEngine().getTextureManager());
		
		try {
			TMXTiledMap map = load.loadFromAsset(FabricaRecursos.get().getMainContext(), "mosaicos/tile_deserto_sala_1_paredes.tmx");
			fundo.attachChild(map.getTMXLayers().get(0));
		} catch (TMXLoadException e) {
			Log.e("TOUPETY", e.getMessage());
			e.printStackTrace();
		}*/
		
		Mosaico mosaico = new CarregadorMosaico().carregarMosaico("tile_deserto_sala_1_paredes", FabricaRecursos.get().getMainContext());		
		mosaico.setPosition(-100,-600);
		
		fundo.attachChild(mosaico);
		
		
		MosaicoAnimado mosaicoAnimado = new CarregadorMosaicoAnimado().carregarMosaico("tile_anim_agua1_a120_c60_1x20", FabricaRecursos.get().getMainContext());
		
		meio.attachChild(mosaicoAnimado);
		
		control = new AnalogOnScreenControl(0, Main.CAMERA_HEIGHT - ctrlTxt.getHeight(), cam, ctrlTxt, ctrlKTxt, 0.1f, this);
//		control.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
//		control.getControlBase().setAlpha(0.5f);
		control.refreshControlKnobPosition();
		
		
//		AnimatedSprite andandoDir = FabricaRecursos.get().getAnimatedSprite(FabricaRecursos.get().getTiledTextureRegion("imagens/toupety/toupety_andando_dir.png", 0, 0, 7, 1, false, 1024, 2048), 0,0,80);
		
//		this.attachChild(andandoDir);
		this.setChildScene(control);
	}

	float px;
	float py;
	
	@Override
	public void onControlChange(BaseOnScreenControl pBaseOnScreenControl, float pValueX, float pValueY) {
		px += pValueX*5;
		py += pValueY*5;
		
		FabricaRecursos.get().getCamera().setCenter(px, py);
	}

	@Override
	public void beginContact(Contact contact) {
		
	}

	@Override
	public void endContact(Contact contact) {
		
	}

	@Override
	public void onControlClick(AnalogOnScreenControl pAnalogOnScreenControl) {
		// TODO Auto-generated method stub
		
	}
}
